package com.zty.bean;

import lombok.Data;

import java.io.Serializable;

/**
 * @Author ZTY
 * @Description 传回给前端的具体内容
 * @Date 1:58 2023-12-12
 **/

@Data
public class Result<T> implements Serializable {

    //    不让其实例化
    private Result(){}

    private Boolean success;

    private Integer code;

    private String message;

    private T data;

    //  无返回数据的Result
    public static Result ok(){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS);
        result.setMessage(ResultMessage.SUCCESS);
        return result;
    }

    public static<T> Result ok(T data){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(ResultCode.SUCCESS);
        result.setMessage(ResultMessage.SUCCESS);
        result.setData(data);
        return result;
    }

    //  有返回数据的Result
    public static Result ok(Integer resultCode,String resultMessage){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(resultCode);
        result.setMessage(resultMessage);
        return result;
    }

    public static<T> Result ok(Integer resultCode,String resultMessage,T data){
        Result result = new Result();
        result.setSuccess(true);
        result.setCode(resultCode);
        result.setMessage(resultMessage);
        result.setData(data);
        return result;
    }







    //  无返回数据的Result
    public static Result error(){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(ResultCode.ERROR);
        result.setMessage(ResultMessage.ERROR);
        return result;
    }

    public static<T> Result error(T data){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(ResultCode.ERROR);
        result.setMessage(ResultMessage.ERROR);
        result.setData(data);
        return result;
    }

    //  有返回数据的Result
    public static Result error(Integer resultCode,String resultMessage){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(resultCode);
        result.setMessage(resultMessage);
        return result;
    }

    public static<T> Result error(Integer resultCode,String resultMessage,T data){
        Result result = new Result();
        result.setSuccess(false);
        result.setCode(resultCode);
        result.setMessage(resultMessage);
        result.setData(data);
        return result;
    }

}
